package com.megvii.corelib.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by linyue on 15/12/25.
 */
public class DateTimeUtil
{
    public static final long TIMESTAMP_FLAG = 10000000000l;

    /**
     * 将timestamp转换成以毫秒为单位的
     *
     * @param timestamp 单位可以是秒或毫秒
     * @return
     */
    public static long toMSTimestamp(long timestamp)
    {
        return timestamp > TIMESTAMP_FLAG ? timestamp : timestamp * 1000;
    }

    /**
     * 将timestamp转换成以秒为单位的
     *
     * @param timestamp 单位可以是秒或毫秒
     * @return
     */
    public static long toSTimestamp(long timestamp)
    {
        return timestamp < TIMESTAMP_FLAG ? timestamp : timestamp / 1000;
    }

    public static String getFormattedDate(long timestamp)
    {
        if(timestamp <= 0) return "";
        return getFormattedDateTime(timestamp, "yyyy-MM-dd");
    }

    public static String getFormattedTime(long timestamp, boolean containsSeconds)
    {
        if (containsSeconds)
        {
            return getFormattedDateTime(timestamp, "HH:mm:ss");
        }
        else
        {
            return getFormattedDateTime(timestamp, "HH:mm");
        }
    }

    public static String getFormattedDateTime(long timestamp, boolean containsSeconds)
    {
        if (containsSeconds)
        {
            return getFormattedDateTime(timestamp, "yyyy-MM-dd HH:mm:ss");
        }
        else
        {
            return getFormattedDateTime(timestamp, "yyyy-MM-dd HH:mm");
        }
    }

    private static long getTimestampFromDate(String dateStr, String formatString)
    {
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        long timestamp = 0;
        try
        {
            timestamp = format.parse(dateStr).getTime();
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return timestamp;
    }

    private static String getFormattedDateTime(long timestamp, String formatString)
    {
        Date date = new Date(timestamp);
        SimpleDateFormat formatter = new SimpleDateFormat(formatString);
        String data = formatter.format(date);
        return data;
    }

    public static long getTimestampFromFormattedDate(String dateStr)
    {
        return getTimestampFromDate(dateStr, "yyyy-MM-dd");
    }

    public static long getTimestampFromFormattedDateTime(String dateStr, boolean containsSeconds)
    {
        String formatStr = containsSeconds ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd HH:mm";
        return getTimestampFromDate(dateStr, formatStr);
    }

    /**
     * 将number前补0，达到formatLength位</br>
     * 如果number本就超过formatLength位，则不变</br>
     * 如果number为负数，也不变
     * @param number
     * @param formatLength
     * @return
     */
    public static String formatNumber(int number, int formatLength)
    {
        if (number < 0)
        {
            return String.valueOf(number);
        }
        else if (number == 0)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < formatLength; i++)
            {
                builder.append("0");
            }
            return builder.toString();
        }
        else
        {
            int originalLength = (int) Math.log10(number) + 1;
            StringBuilder builder = new StringBuilder();
            for (int i = originalLength; i < formatLength; i++)
            {
                builder.append("0");
            }
            builder.append(String.valueOf(number));
            return builder.toString();
        }
    }
}
